package com.tao.controller;

import com.alibaba.fastjson.JSON;
import com.aliyun.oss.common.utils.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tao.constants.SystemConstant;
import com.tao.dto.HelpDto;
import com.tao.entity.Category;
import com.tao.entity.Help;
import com.tao.enums.AppHttpCodeEnum;
import com.tao.service.HelpService;
import com.tao.service.ListService;
import com.tao.utils.BeanCopyUtils;
import com.tao.utils.RedisCache;
import com.tao.utils.SecurityUtils;
import com.tao.vo.ListDetailVo;
import com.tao.vo.ListVo;
import com.tao.vo.ResponseResult;
import io.netty.util.internal.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.web.bind.annotation.*;
import reactor.util.annotation.Nullable;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 *
 * @author 徐书涵
 * @date 2023/5/6
 * @content :help控制类
 */

@RestController
@RequestMapping("")
public class HelpController {
    @Resource
    private HelpService helpService;
    @Resource
    private ListService listService;



    @PostMapping("/help/manage")
    public ResponseResult PublishHelp(@RequestBody HelpDto helpDto) {
        Help help = JSON.parseObject(JSON.toJSONString(helpDto), Help.class);
        help.setCreateBy(SecurityUtils.getUserId());
        helpService.save(help);
        return ResponseResult.okResult(200,"操作成功，发布新的求助！");
    }


    //管理查询求助
    @GetMapping("/help/manage")
    public ResponseResult ManagepageQuery(@RequestParam Map<String,Object> map){
        Map<String,Object> returnData;
        Integer pageNum = Integer.parseInt(map.get("pageNum").toString());
        Integer pageSize = Integer.parseInt(map.get("pageSize").toString());
        returnData = helpService.getHelpByUserId(pageNum, pageSize, SecurityUtils.getUserId());
        return ResponseResult.okResult(returnData);
    }


    @PutMapping("/help/manage")
        public ResponseResult UpdateHelp(@RequestBody HelpDto helpDto){
            Help help = JSON.parseObject(JSON.toJSONString(helpDto), Help.class);
            helpService.updateById(help);
//        helpService.update(help);

            return ResponseResult.okResult(200,"操作成功，求助信息更新！") ;
    }

    @DeleteMapping("/help/manage/{id}")
    public ResponseResult DeleteHelpById(@PathVariable Long id) {
        if (!helpService.deleteByHelpId(id)) {

            return ResponseResult.errorResult(402, "操作失败，存在未完成订单！");
        }
        return ResponseResult.okResult(200, "操作成功，求助及相关订单删除完成！");
    }

    private static RedisCache redisCache;

    @Autowired
    public void setRedisCache(RedisCache redisCache) {
        HelpController.redisCache = redisCache;
    }

    @PutMapping("/help/viewCount/{id}")
    public ResponseResult updateViewCount(@PathVariable Long id) {
        helpService.updateViewCount(id);
        return ResponseResult.okResult();
    }

    //查询全部
    //查询分类
    @GetMapping("/helpList")
    public ResponseResult GetHelpByAll(@RequestParam Map<String, Object> map) {
        Integer pageNum = Integer.parseInt(map.get("pageNum").toString());
        Integer pageSize = Integer.parseInt(map.get("pageSize").toString());
        Map<String, Object> returnData;

        if(StringUtils.isNullOrEmpty((String) map.get("categoryId"))){
            if(StringUtils.isNullOrEmpty((String) map.get("sort"))){
                returnData = listService.getHelpByHelpStatus(pageNum, pageSize,null,"weight");
            }else {
                String sort = map.get("sort").toString();
                if("time".equals(sort)) {
                    sort = "create_time";
                }
                returnData = listService.getHelpByHelpStatus(pageNum, pageSize,null,sort);
            }
        }else{
            if (StringUtils.isNullOrEmpty((String) map.get("sort"))){
                Long categoryId = Long.parseLong(map.get("categoryId").toString());
                returnData= listService.getHelpByHelpStatusAll(pageNum, pageSize, categoryId, "weight");
            }else {
                String sort = map.get("sort").toString();
                if("time".equals(sort)) {
                    sort = "create_time";
                }
                Long categoryId = Long.parseLong(map.get("categoryId").toString());
                returnData= listService.getHelpByHelpStatusAll(pageNum, pageSize, categoryId, sort);
            }
        }
        System.out.println("return:" + returnData);
        return ResponseResult.okResult(returnData);
    }


    //有偿无偿
    @GetMapping("/helpList/hasReward/{hasReward}")
    public ResponseResult GetHelpByReward(@PathVariable Long hasReward){
        List<ListVo> helps=listService.getHelpByReward(hasReward);
        List<ListVo> listVos=BeanCopyUtils.copyBeanList(helps, ListVo.class);
        return ResponseResult.okResult(listVos);
    }

    //搜索求助
    @GetMapping("/helpList/search")
    public ResponseResult GetHelpBySearch(@RequestParam Map<String, Object> map) {
        Integer pageNum = Integer.parseInt(map.get("pageNum").toString());
        Integer pageSize = Integer.parseInt(map.get("pageSize").toString());
        String filter = map.get("filter").toString();
        Map<String, Object> returnData;
        if(StringUtils.isNullOrEmpty((String) map.get("categoryId"))){
            if(StringUtils.isNullOrEmpty((String) map.get("sort"))){
                returnData = listService.getHelpBySearch(pageNum, pageSize,null,"weight",filter);
            }else {
                String sort = map.get("sort").toString();
                if("time".equals(sort))sort = "create_time";
                returnData = listService.getHelpBySearch(pageNum, pageSize,null,sort,filter);
            }
        }else{
            if (StringUtils.isNullOrEmpty((String) map.get("sort"))){
                Long categoryId = Long.parseLong(map.get("categoryId").toString());
                returnData= listService.getHelpBySearch(pageNum, pageSize, categoryId, "weight",filter);
            }else {
                String sort = map.get("sort").toString();
                if("time".equals(sort))sort = "create_time";
                Long categoryId = Long.parseLong(map.get("categoryId").toString());
                returnData= listService.getHelpBySearch(pageNum, pageSize, categoryId, sort,filter);
            }
        }
        System.out.println("return:" + returnData);
        return ResponseResult.okResult(returnData);
    }

    //求助详情查询
    @GetMapping("/helpDetail/{id}")
    public ResponseResult GetHelpByDetail(@PathVariable Long id){
        ListDetailVo help=listService.getHelpByDetail(id);
        ListDetailVo listDetailVo=BeanCopyUtils.copyBean(help, ListDetailVo.class);
        return ResponseResult.okResult(listDetailVo);
    }
    @GetMapping("help/creatorInfo/{helpId}")
    public  ResponseResult getCreatorInfo(@PathVariable Long helpId){
        return helpService.getCreatorInfo(helpId);
    }
}
